package com.ytkj.ygAssist.main;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ytkj.ygAssist.server.GetGoodsInfo;
import com.ytkj.ygAssist.server.GetHistoryBuyRecord;
import com.ytkj.ygAssist.server.SelectAssistPublishs;
import com.ytkj.ygAssist.server.util.DateUtil;
import com.ytkj.ygAssist.server.util.HttpGetUtil;
import com.ytkj.ygAssist.tools.CacheData;
import com.ytkj.ygAssist.tools.Config;
import com.ytkj.ygAssist.tools.JFrameListeningInterface;
import com.ytkj.ygAssist.tools.MyLog;

import java.util.List;
import java.util.Map;

public class NowWinningServer {
    public static void getNowWinningServer(String goodsID, String period, JFrameListeningInterface foreknowInterface) {
        if (CacheData.getGoodsNameCacheDate(goodsID) == null) {
            if (GetGoodsInfo.getGoodsInfoByGoodsID(goodsID)) {
                foreknowInterface.setFrameText("setGoodsName", CacheData.getGoodsNameCacheDate(goodsID));
            } else {
                foreknowInterface.setFrameText("setGoodsNameError", null);
                return;
            }
        } else {
            CacheData.setGoodsTreeListCacheDate(goodsID);
            foreknowInterface.setFrameText("setGoodsName", CacheData.getGoodsNameCacheDate(goodsID));
        }
        String[] cacheDate = CacheData.getSelectCacheDate(goodsID, period);
        if (cacheDate == null) {
            String[] content = GetGoodsInfo.getGoodsPeriodInfo(goodsID, period);
            if (content != null) {
                if (CacheData.getCodeRNOCacheDate(content[1]) == null) {
                    if (content[0].equals("1")) {
                        String[] text = new String[]{goodsID, period, "正在云购中"};
                        foreknowInterface.setFrameListeningText(null, text);
                    } else if (content[0].equals("2")) {
                        SelectAssistPublishs.getYungouPublishs(goodsID, period, "1");
                        if (CacheData.getSelectCacheDate(goodsID, period) == null) {
                            String[] buyTime = getBuyTime(goodsID, period, content[1]);
                            if (buyTime != null) {
                                getCodeRNO(buyTime[1], goodsID, period, content[1], foreknowInterface);
                            } else {
                                foreknowInterface.setFrameText("selectError", null);
                            }
                        } else {
                            foreknowInterface.setFrameListeningText(null,
                                    CacheData.getSelectCacheDate(goodsID, period));
                        }
                    } else if (content[0].equals("3")) {
                        SelectAssistPublishs.getYungouPublishs(goodsID, period, "1");
                        String[] selectCache = CacheData.getSelectCacheDate(goodsID, period);
                        if (selectCache == null) {
                            Map<String, String> barcodernoInfo = GetGoodsInfo.getBarcodernoInfo(content[1]);
                            String[] text = new String[]{goodsID, period, barcodernoInfo.get("codeRNO"), null, null,
                                    null, null, content[1]};
                            foreknowInterface.setFrameListeningText(null, text);
                        } else {
                            foreknowInterface.setFrameListeningText(null, selectCache);
                        }
                    } else {
                        foreknowInterface.setFrameText("selectError", null);
                    }
                } else {
                    String[] text = new String[]{goodsID, period, CacheData.getCodeRNOCacheDate(content[1]), null,
                            null, null, null, content[1]};
                    foreknowInterface.setFrameListeningText(null, text);
                }
            } else {
                foreknowInterface.setFrameText("selectError", null);
            }
        } else {
            foreknowInterface.setFrameListeningText(null, cacheDate);
        }
    }

    /*
     * 获取最后购买时间
     */
    public static String[] getBuyTime(String goodsID, String period, String codeID) {
        if (CacheData.getGoodsPriceCacheDate(goodsID) == null) {
            try {
//                CacheData.setGoodsPriceCacheDate(goodsID,
//                        Integer.parseInt(GetGoodsInfo.shopCartNew(codeID, HttpGetUtil.getHttpClient())[3]));
//                CacheData.setGoodsPriceCacheDate(goodsID,
//                        Integer.parseInt(GetGoodsInfo.shopCartNewByHtml(goodsID, HttpGetUtil.getHttpClient())[3]));
                CacheData.setGoodsPriceCacheDate(goodsID,
                		Integer.parseInt(GetGoodsInfo.getGoodsSalingByGoodsID(goodsID, HttpGetUtil.getHttpClient())[3]));
            } catch (Exception e) {
            }
        }
        String content = GetGoodsInfo.getUserBuyListEnd(codeID, 1, HttpGetUtil.getHttpClient());
        MyLog.outLog("查询到第一页参与记录", goodsID, period, content);
        try {
//            String data = content.substring(content.indexOf("Rows\":") + 6, content.length() - 5);
            String data = content.substring(content.indexOf("data\":") + 6, content.length() - 2);
            List<Map<String, String>> dList = new Gson().fromJson(data, new TypeToken<List<Map<String, String>>>() {
            }.getType());
            String[] s = new String[]{dList.get(0).get("buyID"), dList.get(0).get("buyTime")};
            return s;
        } catch (Exception e) {
        }
        return null;
    }

    /*
     * 获取中奖码
     */
    private static void getCodeRNO(final String buyTime, final String goodsID, final String period, final String codeID,
                                   final JFrameListeningInterface foreknowInterface) {
        MyLog.outLog("正在查询到中奖码", goodsID, period, buyTime);
        new Thread(new Runnable() {
            public void run() {
                long buyTimeLong = DateUtil.getTimeLong(buyTime);
                String selectBuyTime = buyTime;
                while (CacheData.getCodeRNOCacheDate(codeID) == null) {
                    if ((Config.yungouTime - buyTimeLong) > 10000) {
                        MyLog.outLog("检查最后一条记录查询是否错误", goodsID, period, buyTime);
                        String[] checkTime = getBuyTime(goodsID, period, codeID);
                        if (!selectBuyTime.equals(buyTime)) {
                            MyLog.outLog("检查到最后一条记录查询错误了", goodsID, period, checkTime);
                            selectBuyTime = checkTime[1];
                        }
                        String codeRNO = GetHistoryBuyRecord.getCodeRNO(checkTime[0], selectBuyTime,
                                CacheData.getGoodsPriceCacheDate(goodsID).toString());
                        if (codeRNO != null) {
                            CacheData.addCodeRNOCacheDate(codeID, codeRNO);
                            String[] text = new String[]{goodsID, period, codeRNO, "", "", "", "", codeID};
                            foreknowInterface.setFrameListeningText(null, text);
                            MyLog.outLog("查询到中奖码", goodsID, period, codeRNO);
                        } else {
                            MyLog.outLog("查询中奖码错误了", goodsID, period);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        MyLog.outLog("服务器时间未到");
                        MyLog.threadSleep(1000);
                    }
                }
            }
        }).start();
    }

    /*
     * 获取总页数
     */
    private static int getUserBuyListCount(String content) {
        String data = content.split("Count\":", 2)[1];
        data = data.split(",\"Data", 2)[0];
        return Integer.parseInt(data);
    }
}
